package st.business.tepth.business.visitor;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.business.bean.CommonBean;
import st.business.tepth.business.bean.FactoryInfo;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;
import st.shadow.library.core.util.utilcode.util.DeviceUtils;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;

/**
 * @author zhangyu
 * @date 2019/2/18
 */
public class DialogBindFactory extends Dialog {
    private Disposable mDisposable;

    public DialogBindFactory(Context context,
                             final List<FactoryInfo.Factory> factoryList,
                             final OnConfirmListener listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_bind_factory, null);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        final MaterialSpinner spGroup = view.findViewById(R.id.sp_group);
        final MaterialSpinner spFactory = view.findViewById(R.id.sp_factory);
        spFactory.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                FactoryInfo.Factory factory =
                        (FactoryInfo.Factory) item;
                if (factory != null) {
                    List<CommonBean> groupList = factory.getGroupList();
                    if (groupList == null) {
                        groupList = new ArrayList<>();
                    }
                    spGroup.setItems(groupList);
                }
            }
        });

        final TextView tvLoading = view.findViewById(R.id.tv_loading);
        if (factoryList == null || factoryList.isEmpty()) {
            mDisposable = RxRestClient.builder()
                    .url(Url.GetDeviceBindInfo)
                    .params("workbenchNo", DeviceUtils.getAndroidID())
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            FactoryInfo factoryInfo = convertToFactory(response);
                            if (factoryInfo != null) {
                                List<FactoryInfo.Factory> all =
                                        factoryInfo.getAllFactory();
                                if (all != null && !all.isEmpty()) {
                                    spFactory.setItems(all);
                                    List<CommonBean> list =
                                            all.get(0).getGroupList();
                                    if (list == null) {
                                        list = new ArrayList<>();
                                    }
                                    spGroup.setItems(list);
                                }
                            }
                            tvLoading.setVisibility(View.GONE);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {
                            tvLoading.setText(msg);
                        }
                    })
                    .build()
                    .post();
        } else {
            tvLoading.setVisibility(View.GONE);
            spFactory.setItems(factoryList);
        }


        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    List<FactoryInfo.Factory> factories = spFactory.getItems();
                    FactoryInfo.Factory factory = null;
                    if (factories != null && !factories.isEmpty()) {
                        int factoryIndex = spFactory.getSelectedIndex();
                        if (factoryIndex < factories.size()) {
                            factory = factories.get(factoryIndex);
                        }
                    }
                    List<CommonBean> groupList = spGroup.getItems();
                    CommonBean group = null;
                    if (groupList != null && !groupList.isEmpty()) {
                        int groupIndex = spGroup.getSelectedIndex();
                        if (groupIndex < groupList.size()) {
                            group = groupList.get(groupIndex);
                        }
                    }
                    listener.onChoose(factory, group);
                }
                dismiss();
            }
        });
        setContentView(view);
        setCanceledOnTouchOutside(false);
    }

    private FactoryInfo convertToFactory(String response) {
        return new ObjConverter<FactoryInfo>().convert(response
                , new TypeReference<FactoryInfo>() {
                });
    }

    public Dialog measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = ScreenUtils.getScreenWidth() * 800 / 1280; // 宽度
            lp.height = ScreenUtils.getScreenHeight() * 600 / 1024; // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        return this;
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }

    public static interface OnConfirmListener {
        public void onChoose(FactoryInfo.Factory factory,
                             CommonBean commonBean);
    }

}
